package LC;

import java.util.LinkedList;
import java.util.List;

/**
 * https://leetcode.com/problems/pascals-triangle/description/
 * Given numRows, generate the first numRows of Pascal's triangle.
 * For example, given numRows = 5,
 * Return
 * [
 * [1],
 * [1,1],
 * [1,2,1],
 * [1,3,3,1],
 * [1,4,6,4,1]
 * ]
 */
public class LC_118_PascalsTriangle_Math_Loop {
    public static void main(String[] args) {
        List<List<Integer>> lists = Solution.generate(5);
        System.out.println(lists);
    }

    static class Solution {
        static List<List<Integer>> generate(int numRows) {
            List<List<Integer>> rows = new LinkedList<>();
            for (int i = 1; i <= numRows; i++) {
                List<Integer> row = new LinkedList<>();
                if (i == 1) row.add(1);
                if (i == 2) {
                    row.add(1);
                    row.add(1);
                }
                if (i >= 3) {
                    for (int j = 0; j < i; j++) {
                        if (j == 0) {
                            row.add(1);
                        } else if (j == i - 1) {
                            row.add(1);
                        } else if (j != i - 1) {
                            List<Integer> list = rows.get(i - 2);
                            row.add(list.get(j - 1) + list.get(j));
                        }
                    }
                }
                rows.add(row);
            }
            return rows;
        }
    }
}